﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FixedPropertyManagement.Models;

namespace FixedPropertyManagement.aspxs
{
    public partial class login : System.Web.UI.Page
    {
        PropertyMangementEntities3 db = new PropertyMangementEntities3();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        //登录
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string num = txtNum.Text.Trim();
            string pwd = txtPassword.Text.Trim();
            if (num.Equals(""))
            {
                txtNum.Focus();
                return;
            }
            if (pwd.Equals(""))
            {
                txtPassword.Focus();
                return;
            }
            var result = db.Employees.Where(ee => ee.EmpId.ToString() == num).FirstOrDefault();
            if(result != null)
            {
                bool falg = false;
                if (result.DepartId == 520)
                {
                    if (ddlType.SelectedValue != "1")
                    {
                        Response.Write("<script>alert('用户类型不正确！')</script>");
                        ddlType.SelectedValue = "1";
                        txtPassword.Focus();
                        return;
                    }
                    else
                    {
                        falg = true;
                    }

                }else
                {
                    if (ddlType.SelectedValue != "0")
                    {
                        Response.Write("<script>alert('用户类型不正确！')</script>");
                        ddlType.SelectedValue = "0";
                        txtPassword.Focus();
                        return;
                    }else falg = true;
                }
                if(falg)
                {
                    if (result.Password.Trim().ToString().Equals(pwd))
                    {
                        var de = db.Departments.Where(d => d.DepartId == result.DepartId).FirstOrDefault();
                        Session["userNum"] = num;
                        if(de!=null) Session["userType"] = de.Name.Trim();
                        Session["userName"] = result.Name.ToString().Trim();
                        if(result.IsManger == true)
                            Session["userMange"] = "是";
                        else Session["userMange"] = "否";
                        Response.Write("<script>alert('欢迎" + result.Name.Trim() + "使用固定资源管理系统！"+de.Name.Trim()+"')</script>");
                        Response.Write("<script>window.location = '../default.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('密码不正确！')</script>");
                        txtPassword.Text = "";
                        txtPassword.Focus();
                    }
                }                               
            }
            else
            {
                Response.Write("<script>alert('系统中不存在该用户！')</script>");
                txtNum.Focus();
            }

        }
        //注册
        protected void btnRegired_Click(object sender, EventArgs e)
        {
            Response.Redirect("register.aspx");
        }
    }
}